package com.cxh.rmsq.ui.activity.self

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProviders
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.cxh.rmsq.R
import com.cxh.rmsq.common.Constant
import com.cxh.rmsq.db.model.AuthenticationBean
import com.cxh.rmsq.im.IMManager
import com.cxh.rmsq.model.Result2
import com.cxh.rmsq.ui.BaseActivity
import com.cxh.rmsq.ui.activity.WebViewActivity
import com.cxh.rmsq.viewmodel.SecurityViewModel
import io.rong.callkit.util.SPUtils

class SecuritySettingsActivity : BaseActivity(), View.OnClickListener {

    private val tvAuthentication: TextView by lazy { findViewById<TextView>(R.id.tv_authentication) }
    private val ll_zjxx: LinearLayout by lazy { findViewById<LinearLayout>(R.id.ll_zjxx) }
    private var securityViewModel: SecurityViewModel? = null
    var status = ""
    private var databean = AuthenticationBean()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_security_settings)
        findViewById<ConstraintLayout>(R.id.cl_toolbar).setPadding(0, 0, 0, 0)
//        findViewById<ConstraintLayout>(R.id.cl_toolbar).setPadding(0, BarUtils.getStatusBarHeight(), 0, 0)
        BarUtils.setStatusBarColor(this, ColorUtils.getColor(R.color.transparent))
        BarUtils.setStatusBarLightMode(this, true)

        findViewById<LinearLayout>(R.id.iv_back).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.ll_pwd_edit).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.ll_mobile_bind).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.ll_zjxx).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.ll_logoff).setOnClickListener(this)
        findViewById<LinearLayout>(R.id.ll_child).setOnClickListener(this)

        securityViewModel = ViewModelProviders.of(this).get(SecurityViewModel::class.java)
        ll_zjxx.isVisible = false
    }

    override fun onResume() {
        super.onResume()
        securityViewModel!!.getAuthentication(IMManager.getInstance().currentId)
        securityViewModel!!.authentication.observe(this@SecuritySettingsActivity) { resource: Result2<AuthenticationBean?> ->
//            if (resource.status == Status.SUCCESS && resource.data != null) {
            if (resource.result != null) {
                databean = resource.result!!
                status = resource.result!!.status.toString()
                SPUtils.put(this, Constant.AuthenticationStatus, status)
                when (resource.result!!.status) {
                    "1" -> tvAuthentication.setText("审核中")
                    "2" -> tvAuthentication.setText("已通过")
                    "3" -> tvAuthentication.setText("已拒绝")
                }
            } else {
                tvAuthentication.text = "去提交"
            }
//            }
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.ll_child -> {
                startActivity(
                    Intent(this, UnderModeActivity::class.java) )
            }

            R.id.iv_back -> {
                finish()
            }

            R.id.ll_pwd_edit -> {
                startActivity(
                    Intent(this, ForgetPwdActivity::class.java)
                        .putExtra("isEdit", "1")
                )
            }

            R.id.ll_mobile_bind -> {
                startActivity(Intent(this, BindPhoneActivity::class.java))
            }

            R.id.ll_logoff -> {
                startActivity(
                    Intent(
                        this,
                        WebViewActivity::class.java
                    ).putExtra(WebViewActivity.PARAMS_URL, Constant.logoff)
                        .putExtra(WebViewActivity.PARAMS_TITLE, "账号注销")
                )
            }

            R.id.ll_zjxx -> {
                if ("2" == status) {
                    startActivity(
                        Intent(
                            this@SecuritySettingsActivity,
                            AuthenticationSucessActivity::class.java
                        ).putExtra("dataBean", databean)
                    )
                } else {
                    startActivity(
                        Intent(
                            this@SecuritySettingsActivity,
                            AuthenticationActivity::class.java
                        )
                    )
                }
            }
        }
    }
}